package com.bb.blog.code.threading.example;

import java.util.ArrayList;
import java.util.List;

public class VolatileCounterExample {
    private static volatile int i = 0;
    private static Object lock = new Object();

    public static void main(String[] args) throws InterruptedException {
        List<Thread> threadList = new ArrayList<>();
        for (int j = 0; j < 10; j++) {
            Thread t = new Thread(() -> {
                for (int k = 0; k < 10000; k++) {
//                    synchronized (lock) {

                    i++;

//                    }
                }
            });
            threadList.add(t);
        }
        threadList.forEach(Thread::start);

        threadList.forEach(t -> {
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        Thread.sleep(1000);
        System.out.println(i);
    }
}
